<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="tong.li&#39;s blog">
  <meta name="keyword" content="彤哥哥博客，95后技术爱好者,现就职于同程旅行/同程艺龙上海分公司，专注于互联网技术分享的平台。">
  
    <link rel="shortcut icon" href="/css/images/icon.png">
  
  <title>
    
      支付重构之路 | 彤哥哥的博客
    
  </title>
  <link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/highlight.js/9.12.0/styles/tomorrow-night.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdn.staticfile.org/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>彤哥哥的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/series/" class="item-link">分类</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">项目</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/series/" class="menu-link">分类</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">项目</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>支付重构之路</h2>
  <p class="post-date">2022-01-06</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>最近闲来无事，学习了设计模式，今天用设计模式重构一下支付相关代码。</p>
<p>本篇主要是利用策略、模板方法两种设计模式来重构代码，重构时遵循开闭、单一职责等原则，做到低耦合、高内聚，为后续代码的可维护性打下良好的基础。</p>
<p>在重构之前，我们经过会见到如下这种if-else型的支付伪代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">pay</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">// 支付结果</span></span><br><span class="line">    <span class="type">boolean</span> <span class="variable">payResult</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (微信支付) &#123;</span><br><span class="line">        payResult = doWeChatPay(支付参数);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (支付宝支付) &#123;</span><br><span class="line">        payResult = doAliPay(支付参数);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(银联支付) &#123;</span><br><span class="line">        payResult = doUnionPay(支付参数);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> payResult ? <span class="string">&quot;支付成功&quot;</span> :<span class="string">&quot;支付失败&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>上述代码，虽然能够解决当时的支付需求。但是随着项目的迭代越来越多，上述代码变的更难维护。</p>
<p>if-else代码堆积的太多，就变成了我们俗称的“屎山”，改动时很容易“牵一发而动全身”，至此这段代码我们要趁早优化掉，防止系统在某时某刻出现不可预知的故障，避免给个人和公司造成不可挽回的损失。</p>
<h1 id="重构"><a href="#重构" class="headerlink" title="重构"></a>重构</h1><p>我们首先定义一下支付时域的基本模型：请求支付模型、支付枚举和支付方式注解。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> model.dto;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: model.dto.BasePayDTO</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 基础支付模型实体</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:14</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BasePayDTO</span>&lt;T&gt; <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付方式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String method;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付数据域</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> T  data;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付配置</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> T  configInfo;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> model.enums;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@enumName</span>: model.enums.PayMethodEnum</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付方式枚举</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 15:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> <span class="title class_">PayMethodEnum</span> &#123;</span><br><span class="line"></span><br><span class="line">    WECHAT(<span class="string">&quot;WeChat&quot;</span>,<span class="string">&quot;微信支付&quot;</span>),</span><br><span class="line">    ALI(<span class="string">&quot;Ali&quot;</span>,<span class="string">&quot;支付宝支付&quot;</span>),</span><br><span class="line">    Union(<span class="string">&quot;Union&quot;</span>,<span class="string">&quot;银联支付&quot;</span>)</span><br><span class="line">    ;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String method;</span><br><span class="line">    <span class="keyword">private</span> String desc;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getMethod</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> method;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getDesc</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> desc;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    PayMethodEnum(String method, String desc) &#123;</span><br><span class="line">        <span class="built_in">this</span>.method = method;</span><br><span class="line">        <span class="built_in">this</span>.desc = desc;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> model.annotations;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> model.enums.PayMethodEnum;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@annotationName</span>: model.annotations.PayMode</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付方式注解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:33</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Target(&#123;ElementType.TYPE&#125;)</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> PayMode &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> String&#125;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    PayMethodEnum <span class="title function_">method</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>紧接着，我们定义一下支付逻辑抽象层的方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@interfaceName</span>: core.service.IPayService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付逻辑接口抽象层</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:22</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">IPayService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付预处理(可选)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> basePayDTO</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    &lt;T&gt; <span class="keyword">void</span> <span class="title function_">preHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 执行支付</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> basePayDTO</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    &lt;T&gt; <span class="type">boolean</span> <span class="title function_">invoke</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付成功后处理逻辑</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> basePayDTO</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    &lt;T&gt; <span class="keyword">void</span> <span class="title function_">successHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付失败后处理逻辑</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> basePayDTO</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    &lt;T&gt; <span class="keyword">void</span> <span class="title function_">failHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取支付配置信息(如果各个支付方式的配置类似，该方法可进行向上抽取)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    Object <span class="title function_">getPayConfigInfo</span><span class="params">()</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>定义好上述抽象方法后，我们先实现一个微信支付服务：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> model.annotations.PayMode;</span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"><span class="keyword">import</span> model.enums.PayMethodEnum;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.WeChatPayImpl</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 微信支付逻辑处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@PayMode(method = PayMethodEnum.WECHAT)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">WeChatPayImpl</span>  <span class="keyword">implements</span> <span class="title class_">IPayService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> LoggerFactory.getLogger(WeChatPayImpl.class);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">preHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;微信支付前，准备参数等....&quot;</span> + JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="type">boolean</span> <span class="title function_">invoke</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;获取配置中，请稍等....&quot;</span>+ <span class="built_in">this</span>.getPayConfigInfo());</span><br><span class="line">        logger.info(<span class="string">&quot;微信支付中，请稍等....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">successHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;微信成功....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">failHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;微信失败....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object <span class="title function_">getPayConfigInfo</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;配置数据&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后，我们开始策略处理，先定义支付对象容器，通过Spring BeanProcessor来管理上述实现的支付对象：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.ConcurrentHashMap;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.PayBeanContainer</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付Bean容器</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:36</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PayBeanContainer</span> &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付bean容器</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Map&lt;String, IPayService&gt; CONTAINER = <span class="keyword">new</span> <span class="title class_">ConcurrentHashMap</span>&lt;&gt;(<span class="number">16</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加bean</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> method 支付方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> bean Bean</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addBean</span><span class="params">(String method, IPayService bean)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (checkBean(method)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;已存在该业务规则!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        CONTAINER.put(method, bean);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取Bean</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> method 支付方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> IPayService&#125;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> IPayService <span class="title function_">getBean</span><span class="params">(String method)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!checkBean(method)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;不存在该Bean!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> CONTAINER.get(method);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 检查Bean是否存在</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> method 支付方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> boolean</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">checkBean</span><span class="params">(String method)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> CONTAINER.containsKey(method);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> model.annotations.PayMode;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.PayServiceInstantiationAwareBeanPostProcessor</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付Bean注册逻辑处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:38</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PayServiceInstantiationAwareBeanPostProcessor</span> <span class="keyword">implements</span> <span class="title class_">InstantiationAwareBeanPostProcessor</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> LoggerFactory.getLogger(PayServiceInstantiationAwareBeanPostProcessor.class);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> PayBeanContainer payBeanContainer;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 启动过程实例化后</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> bean</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> beanName</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> boolean</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">postProcessAfterInstantiation</span><span class="params">(Object bean, String beanName)</span> <span class="keyword">throws</span> IllegalStateException&#123;</span><br><span class="line">        <span class="keyword">if</span> (bean <span class="keyword">instanceof</span> IPayService) &#123;</span><br><span class="line">            <span class="type">PayMode</span> <span class="variable">annotation</span> <span class="operator">=</span> bean.getClass().getAnnotation(PayMode.class);</span><br><span class="line">            <span class="keyword">if</span> (annotation == <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(<span class="string">&quot;支付实例必须显示声明支付模型:&quot;</span> +  PayMode.class.getName());</span><br><span class="line">            &#125;</span><br><span class="line">            payBeanContainer.addBean(annotation.method().getMethod(), (IPayService) bean);</span><br><span class="line">            logger.info(<span class="string">&quot;支付实例加载:&#123;&#125;&quot;</span>, bean.getClass().getName());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后，我们通过模板设计模式进行对外暴露支付业务：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.PayTemplate</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付模板，外层调用，屏蔽支付细节</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 14:44</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PayTemplate</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注入支付Bean容器对象</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> PayBeanContainer payBeanContainer;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付全流程</span></span><br><span class="line"><span class="comment">     *  1.校验支付方式合法性</span></span><br><span class="line"><span class="comment">     *  2.获取具体的支付实现实例</span></span><br><span class="line"><span class="comment">     *  3.进行支付预处理</span></span><br><span class="line"><span class="comment">     *  4.执行支付</span></span><br><span class="line"><span class="comment">     *  5.支付执行后处理</span></span><br><span class="line"><span class="comment">     *    5.1 支付成功处理</span></span><br><span class="line"><span class="comment">     *    5.2 支付失败处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> payParams</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="type">boolean</span> <span class="title function_">pay</span><span class="params">(BasePayDTO&lt;T&gt; payParams)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!payBeanContainer.checkBean(payParams.getMethod())) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;不支持该支付方式，非法调用!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">IPayService</span> <span class="variable">server</span> <span class="operator">=</span> payBeanContainer.getBean(payParams.getMethod());</span><br><span class="line">        server.preHandle(payParams);</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">result</span> <span class="operator">=</span> server.invoke(payParams);</span><br><span class="line">        <span class="keyword">if</span> (result) &#123;</span><br><span class="line">            server.successHandle(payParams);</span><br><span class="line">            <span class="keyword">return</span> result;</span><br><span class="line">        &#125;</span><br><span class="line">        server.failHandle(payParams);</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>到这里，我们基本上重构架构搭建好了，大家实现对应的支付逻辑就可以了。</p>
<h1 id="验证"><a href="#验证" class="headerlink" title="验证"></a>验证</h1><p>我们先编写一个Action来测试一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.facade.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSONObject;</span><br><span class="line"><span class="keyword">import</span> core.service.impl.payment.PayTemplate;</span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.*;</span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.facade.controller.PayController</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付API</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 15:03</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;pay&quot;)</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PayController</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> PayTemplate payTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping(&quot;/&#123;method&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">pay</span><span class="params">(<span class="meta">@PathVariable(&quot;method&quot;)</span> String method, <span class="meta">@RequestBody</span> JSONObject request)</span> &#123;</span><br><span class="line">        BasePayDTO&lt;JSONObject&gt; payDTO = <span class="keyword">new</span> <span class="title class_">BasePayDTO</span>&lt;&gt;();</span><br><span class="line">        payDTO.setMethod(method);</span><br><span class="line">        payDTO.setData(request);</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">reslult</span> <span class="operator">=</span> payTemplate.pay(payDTO);</span><br><span class="line">        <span class="keyword">if</span> (reslult) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;支付成功&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;支付失败&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>选择微信支付，执行结果为<strong>支付成功</strong>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">请求地址：http://localhost:9090/pay/WeChat</span><br><span class="line">请求参数：</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;李彤&quot;</span>,</span><br><span class="line">    <span class="string">&quot;goodsName&quot;</span>: <span class="string">&quot;小米音箱&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">请求返回：支付成功</span><br></pre></td></tr></table></figure>

<p>执行日志如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">2022-01-06 11:20:47.445 [http-nio-9090-exec-6] INFO: 微信支付前，准备参数等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;WeChat&quot;</span>&#125; [WeChatPayImpl.java:27]</span><br><span class="line">2022-01-06 11:20:47.445 [http-nio-9090-exec-6] INFO: 获取配置中，请稍等....配置数据 [WeChatPayImpl.java:32]</span><br><span class="line">2022-01-06 11:20:47.445 [http-nio-9090-exec-6] INFO: 微信支付中，请稍等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;WeChat&quot;</span>&#125; [WeChatPayImpl.java:33]</span><br><span class="line">2022-01-06 11:20:47.445 [http-nio-9090-exec-6] INFO: 微信成功....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;WeChat&quot;</span>&#125; [WeChatPayImpl.java:39]</span><br></pre></td></tr></table></figure>

<p>我们在选择一个非法支付方式执行一下吧：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">请求地址：http:<span class="comment">//localhost:9090/pay/xxx</span></span><br><span class="line">请求参数：</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;李彤&quot;</span>,</span><br><span class="line">    <span class="string">&quot;goodsName&quot;</span>: <span class="string">&quot;小米音箱&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">请求返回：</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;timestamp&quot;</span>: <span class="string">&quot;2022-01-06T03:34:59.763+0000&quot;</span>,</span><br><span class="line">    <span class="string">&quot;status&quot;</span>: <span class="number">500</span>,</span><br><span class="line">    <span class="string">&quot;error&quot;</span>: <span class="string">&quot;Internal Server Error&quot;</span>,</span><br><span class="line">    <span class="string">&quot;message&quot;</span>: <span class="string">&quot;不支持该支付方式，非法调用!&quot;</span>,</span><br><span class="line">    <span class="string">&quot;path&quot;</span>: <span class="string">&quot;/pay/xxx&quot;</span></span><br><span class="line">&#125;    </span><br></pre></td></tr></table></figure>

<p>执行日志如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">2022-01-06 18:35:20.936 [http-nio-9090-exec-9] ERROR: Servlet.service() <span class="keyword">for</span> servlet [dispatcherServlet] <span class="keyword">in</span> context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: 不支持该支付方式，非法调用!] with root cause [DirectJDKLog.java:175]</span><br><span class="line">java.lang.RuntimeException: 不支持该支付方式，非法调用!</span><br><span class="line">	at core.service.impl.payment.PayTemplate.pay(PayTemplate.java:41)</span><br><span class="line">	at core.facade.controller.PayController.pay(PayController.java:28)</span><br><span class="line">	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)</span><br><span class="line">	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)</span><br><span class="line">	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)</span><br><span class="line">	at java.lang.reflect.Method.invoke(Method.java:498)</span><br></pre></td></tr></table></figure>

<h1 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h1><p>我们再完善支付宝与银联支付方式后再测试一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> core.service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> model.annotations.PayMode;</span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"><span class="keyword">import</span> model.enums.PayMethodEnum;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.AliPayImpl</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 支付宝支付逻辑处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 15:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@PayMode(method = PayMethodEnum.ALI)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AliPayImpl</span> <span class="keyword">implements</span> <span class="title class_">IPayService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> LoggerFactory.getLogger(AliPayImpl.class);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">preHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;支付宝支付前，准备参数等....&quot;</span> + JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="type">boolean</span> <span class="title function_">invoke</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;获取配置中，请稍等....&quot;</span>+ <span class="built_in">this</span>.getPayConfigInfo());</span><br><span class="line">        logger.info(<span class="string">&quot;支付宝支付中，请稍等....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">successHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;支付宝成功....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">failHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;支付宝失败....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object <span class="title function_">getPayConfigInfo</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;配置数据&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> service.impl.payment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"><span class="keyword">import</span> core.service.IPayService;</span><br><span class="line"><span class="keyword">import</span> model.annotations.PayMode;</span><br><span class="line"><span class="keyword">import</span> model.dto.BasePayDTO;</span><br><span class="line"><span class="keyword">import</span> model.enums.PayMethodEnum;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: core.service.impl.payment.UnionPayImpl</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 银联支付支付逻辑处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2022/1/06 15:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@copyright</span>: Tongcheng-Elong Holdings Limited</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@PayMode(method = PayMethodEnum.Union)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UnionPayImpl</span> <span class="keyword">implements</span> <span class="title class_">IPayService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> LoggerFactory.getLogger(UnionPayImpl.class);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">preHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;银联支付支付前，准备参数等....&quot;</span> + JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="type">boolean</span> <span class="title function_">invoke</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;获取配置中，请稍等....&quot;</span>+ <span class="built_in">this</span>.getPayConfigInfo());</span><br><span class="line">        logger.info(<span class="string">&quot;银联支付支付中，请稍等....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">successHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;银联支付成功....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">failHandle</span><span class="params">(BasePayDTO&lt;T&gt; basePayDTO)</span> &#123;</span><br><span class="line">        logger.info(<span class="string">&quot;银联支付失败....&quot;</span>+ JSON.toJSONString(basePayDTO));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object <span class="title function_">getPayConfigInfo</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;配置数据&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">请求地址：http://localhost:9090/pay/Ali</span><br><span class="line">请求参数：</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;李彤&quot;</span>,</span><br><span class="line">    <span class="string">&quot;goodsName&quot;</span>: <span class="string">&quot;小米音箱&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">请求返回：支付成功</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">请求地址：http://localhost:9090/pay/Ali</span><br><span class="line">请求参数：</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;李彤&quot;</span>,</span><br><span class="line">    <span class="string">&quot;goodsName&quot;</span>: <span class="string">&quot;小米音箱&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">请求返回：支付成功</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.320</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: Initializing Spring DispatcherServlet <span class="string">&#x27;dispatcherServlet&#x27;</span> [DirectJDKLog.java:<span class="number">173</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.320</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: Initializing Servlet <span class="string">&#x27;dispatcherServlet&#x27;</span> [FrameworkServlet.java:<span class="number">525</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.345</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: Completed initialization in <span class="number">25</span> ms [FrameworkServlet.java:<span class="number">547</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.592</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: 支付宝支付前，准备参数等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Ali&quot;</span>&#125; [AliPayImpl.java:<span class="number">27</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.593</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: 获取配置中，请稍等....配置数据 [AliPayImpl.java:<span class="number">32</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.593</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: 支付宝支付中，请稍等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Ali&quot;</span>&#125; [AliPayImpl.java:<span class="number">33</span>]</span><br><span class="line"><span class="number">2022</span>-<span class="number">01</span>-<span class="number">06</span> <span class="number">18</span>:<span class="number">41</span>:<span class="number">20.593</span> [http-nio-<span class="number">9090</span>-exec-<span class="number">1</span>] INFO: 支付宝成功....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Ali&quot;</span>&#125; [AliPayImpl.java:<span class="number">39</span>]</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">2022-01-06 18:41:57.870 [http-nio-9090-exec-6] INFO: 银联支付支付前，准备参数等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Union&quot;</span>&#125; [UnionPayImpl.java:27]</span><br><span class="line">2022-01-06 18:41:57.871 [http-nio-9090-exec-6] INFO: 获取配置中，请稍等....配置数据 [UnionPayImpl.java:32]</span><br><span class="line">2022-01-06 18:41:57.871 [http-nio-9090-exec-6] INFO: 银联支付支付中，请稍等....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Union&quot;</span>&#125; [UnionPayImpl.java:33]</span><br><span class="line">2022-01-06 18:41:57.871 [http-nio-9090-exec-6] INFO: 银联支付成功....&#123;<span class="string">&quot;data&quot;</span>:&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李彤&quot;</span>,<span class="string">&quot;goodsName&quot;</span>:<span class="string">&quot;小米音箱&quot;</span>&#125;,<span class="string">&quot;method&quot;</span>:<span class="string">&quot;Union&quot;</span>&#125; [UnionPayImpl.java:39]</span><br></pre></td></tr></table></figure>

<p>好了，本期的重构在这里已经结束了，相信大家对重构有个更深入的理解，感谢大家阅读。</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#设计模式" >
    <span class="tag-code">设计模式</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2021/12/31/%E4%BD%BF%E7%94%A8iText%E5%BA%93%E7%94%9F%E6%88%90PDF%E6%8A%A5%E8%A1%A8/">
        <span class="nav-arrow">← </span>
        
          使用iText库生成PDF报表
        
      </a>
    
    
      <a class="nav-right" href="/2022/01/23/%E3%80%90%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%E3%80%91%E4%B8%8D%E7%9F%A5%E6%89%80%E6%8E%AA%E7%9A%84%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/">
        
          【数据结构与算法】不知所措的二分查找
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="ltyeamin/blogtalks"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-nav-text">前言</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#%E9%87%8D%E6%9E%84"><span class="toc-nav-text">重构</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#%E9%AA%8C%E8%AF%81"><span class="toc-nav-text">验证</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#%E6%89%A9%E5%B1%95"><span class="toc-nav-text">扩展</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://example.com/2022/01/06/支付重构之路/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2024 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a target="_blank" rel="noopener" href="https://github.com/ltyeamin">tong.li</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("https://cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("https://cdn.staticfile.org/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->



<script src='https://cdn.staticfile.org/mermaid/8.11.2/mermaid.min.js'></script>



<script src="/js/script.js"></script>


  </body>
</html>